package com.srd.phone.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.shengrongdai.srd.R;
import com.srd.phone.application.AppActivityManager;
import com.srd.phone.fragment.TabBorrowFragment;
import com.srd.phone.fragment.TabHomeFragment;
import com.srd.phone.fragment.TabMineFragment;
import com.srd.phone.fragment.TabTenderListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager vp;
    private RadioGroup rg_home;

    private List<Fragment> list = new ArrayList<>();
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    public void initView() {
        fl_common_head.setVisibility(View.GONE);
        vp = (ViewPager) findViewById(R.id.vp);
        rg_home = (RadioGroup) findViewById(R.id.rg_home);
        rg_home.check(R.id.rb_home);
        findViewById(R.id.rb_home).setOnClickListener(this);
        findViewById(R.id.rb_invest).setOnClickListener(this);
        findViewById(R.id.rb_borrow).setOnClickListener(this);
        findViewById(R.id.rb_oneself).setOnClickListener(this);
    }

    public void initData() {
        list.add(new TabHomeFragment());
        list.add(new TabTenderListFragment());
        list.add(new TabBorrowFragment());
        list.add(new TabMineFragment());
        vp.setOffscreenPageLimit(3);
        vp.setAdapter(new MainPageAdatper(getSupportFragmentManager()));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rg_home.check(R.id.rb_home);
                } else if (position == 1) {
                    rg_home.check(R.id.rb_invest);
                } else if (position == 2) {
                    rg_home.check(R.id.rb_borrow);
                } else if (position == 3) {
                    rg_home.check(R.id.rb_oneself);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setCurrentItem(int position) {
        if (position == 0) {
            rg_home.check(R.id.rb_home);
        } else if (position == 1) {
            rg_home.check(R.id.rb_invest);
        } else if (position == 2) {
            rg_home.check(R.id.rb_borrow);
        } else if (position == 3) {
            rg_home.check(R.id.rb_oneself);
        }
        vp.setCurrentItem(position, false);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rb_home) {
            setCurrentItem(0);
        } else if (v.getId() == R.id.rb_invest) {
            setCurrentItem(1);
        } else if (v.getId() == R.id.rb_borrow) {
            setCurrentItem(2);
        } else if (v.getId() == R.id.rb_oneself) {
//            if (!getLoginStatus()) {
//                startActivity(new Intent(mContext, LoginActivity.class));
//            } else {
                setCurrentItem(3);
//            }
        }
    }


    class MainPageAdatper extends FragmentPagerAdapter {

        public MainPageAdatper(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycle(null, new String[]{""});
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis() - exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                AppActivityManager.getInstance().removeAllActivity();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
